package com.android.baselibrary.widget.MRecylcer.mrecyclerview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.baselibrary.R;
import com.android.baselibrary.util.ScreenUtil;


public class LoadingMoreFooter extends LinearLayout {

    private SimpleViewSwithcer progressCon;
    private Context mContext;
    public final static int STATE_LOADING = 0;
    public final static int STATE_COMPLETE = 1;
    public final static int STATE_NOMORE = 2;
    public final static int STATE_RELOAD = 3;
    public final static int STATE_NORMAL = 4;
    private ReloadOnClick reloadOnClick;
    private TextView mText;
    private int state;
    private TextView reloadText;

    public LoadingMoreFooter(Context context) {
        super(context);
        initView(context);
    }

    /**
     * @param context
     * @param attrs
     */
    public LoadingMoreFooter(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public void initView(Context context) {
        mContext = context;
        setGravity(Gravity.CENTER);
        setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT
                , ScreenUtil.dip2px(mContext, 40)));
        progressCon = new SimpleViewSwithcer(context);
        progressCon.setLayoutParams(new ViewGroup.LayoutParams(
                ScreenUtil.dip2px(mContext, 20), ScreenUtil.dip2px(mContext, 20)));

        ProgressBar progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyle);
        progressBar.setIndeterminate(true);
        progressCon.setView(progressBar);

        addView(progressCon);
        mText = new TextView(context);
        reloadText = new TextView(context);
        reloadText.setTextColor(Color.parseColor("#FFFFFFFF"));
        mText.setText("正在加载...");

        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins((int) getResources().getDimension(R.dimen.textandiconmargin), 0, 0, 0);

        mText.setLayoutParams(layoutParams);
        reloadText.setLayoutParams(layoutParams);
        addView(mText);
        addView(reloadText);

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (state) {
                    case STATE_LOADING:

                        break;
                    case STATE_COMPLETE:

                        break;
                    case STATE_NOMORE:
                        break;
                    case STATE_RELOAD:
                        if (reloadOnClick != null)
                            reloadOnClick.reloadOnClick(v);
                        break;
                }

            }
        });
    }

    public void setProgressStyle(int style) {
        progressCon.setView(new ProgressBar(mContext, null, android.R.attr.progressBarStyle));

//        if(style == ProgressStyle.SysProgress){

//        }else{
//            AVLoadingIndicatorView progressView = new  AVLoadingIndicatorView(this.getContext());
//            progressView.setIndicatorColor(0xffB5B5B5);
//            progressView.setIndicatorId(style);
//            progressCon.setView(progressView);
//        }
    }

    public void setState(int state) {
        this.state = state;
        switch (state) {
            case STATE_LOADING:
                progressCon.setVisibility(View.VISIBLE);
                mText.setText(mContext.getText(R.string.listview_loading));
                mText.setVisibility(VISIBLE);
                this.setVisibility(View.VISIBLE);
                reloadText.setVisibility(GONE);
                break;
            case STATE_COMPLETE:
                mText.setText(mContext.getText(R.string.listview_loading));
                mText.setVisibility(VISIBLE);
                reloadText.setVisibility(GONE);
                progressCon.setVisibility(View.VISIBLE);
                this.setVisibility(View.GONE);

                break;
            case STATE_NOMORE:
                mText.setText("没有更多了");
                progressCon.setVisibility(View.GONE);
                this.setVisibility(View.VISIBLE);
                reloadText.setVisibility(GONE);
                break;
            case STATE_RELOAD:
                progressCon.setVisibility(View.GONE);
                mText.setVisibility(View.GONE);
                reloadText.setVisibility(VISIBLE);
                reloadText.setText("加载失败，点击重新加载");
                this.setVisibility(View.VISIBLE);
                break;
            case STATE_NORMAL:
                progressCon.setVisibility(View.GONE);
                mText.setVisibility(GONE);
                reloadText.setVisibility(GONE);
                this.setVisibility(View.GONE);
                break;
        }

    }

    public void setReloadOnClickListener(ReloadOnClick mReloadOnClick) {
        reloadOnClick = mReloadOnClick;
    }

    public interface ReloadOnClick {
        void reloadOnClick(View view);
    }
}
